<template>
  <comcard>
    <div slot="title_name" class="shop">
      <div>店铺管理</div>
      <div>
        <el-button type="primary" size="small" @click="update()"
          >保存</el-button
        >
      </div>
    </div>
    <div slot="body_name">
      <el-row>
        <el-col :md="12">
          <el-form :model="formObj" label-width="100px">
            <el-form-item label="店铺名称">
              <el-input type="text" v-model="formObj.name"></el-input>
            </el-form-item>
            <el-form-item label="店铺公告">
              <el-input type="textarea" v-model="formObj.bulletin"></el-input>
            </el-form-item>
            <el-form-item label="店铺头像">
              <el-upload
                class="avatar-uploader"
                action="http://localhost:5000/shop/upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="headerUrl" :src="headerUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="店铺图片">
              <el-upload
                action="http://localhost:5000/shop/upload"
                list-type="picture-card"
                :file-list="fileList"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :on-success="shopImg_success"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt />
              </el-dialog>
            </el-form-item>
            <el-form-item label="配送费">
              <el-input type="text" v-model="formObj.deliveryPrice"></el-input>
            </el-form-item>
            <el-form-item label="配送时间">
              <el-input type="text" v-model="formObj.deliveryTime"></el-input>
            </el-form-item>
            <el-form-item label="配送描述">
              <el-input type="text" v-model="formObj.description"></el-input>
            </el-form-item>
            <el-form-item label="评分">
              <el-input type="text" v-model="formObj.score"></el-input>
            </el-form-item>
            <el-form-item label="销量">
              <el-input type="text" v-model="formObj.sellCount"></el-input>
            </el-form-item>
            <el-form-item label="活动">
              <el-checkbox-group v-model="formSupports">
                <el-checkbox label="满28减5" name="type"></el-checkbox>
                <el-checkbox
                  label="VC无限橙果汁全场8折"
                  name="type"
                ></el-checkbox>
                <el-checkbox label="精彩套餐" name="type"></el-checkbox>
                <el-checkbox label="特价八折" name="type"></el-checkbox>
                <el-checkbox label="单人精彩套餐" name="type"></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-form-item label="营业时间">
              <el-time-picker
                is-range
                v-model="startTime"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                placeholder="选择时间范围"
              ></el-time-picker>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </comcard>
</template>

<script>
import comcard from "../../components/comcard/index.vue";
import { api_updata, apii_deilta } from "../../api/shop";
export default {
  components: {
    comcard,
  },
  data() {
    return {
      // 时间范围
      startTime: [],
      // 店铺头像
      headerUrl: "",
      formObj: {
        id: "",
        name: "",
        bulletin: "",
        avatar: "",
        deliveryPrice: "",
        deliveryTime: "",
        description: "",
        score: "",
        sellCount: "",
        supports: "",
        date: "",
        pics: [],
      },
      //   绑定活动复选框
      formSupports: [],
      //   预览的图片地址
      dialogImageUrl: "",
      //    控制预览的弹窗显示
      dialogVisible: false,
      // 图片列表
      fileList: [],
    };
  },
  mounted() {
    this.query();
  },
  methods: {
    async update() {
      this.formObj.avatar = this.headerUrl.substr(
        this.headerUrl.lastIndexOf("/") + 1
      );
      this.formObj.pics = this.fileList.map((item) => {
        return item.name;
      });
      this.formObj.pics = JSON.stringify(this.formObj.pics);
      this.formObj.supports = JSON.stringify(this.formSupports);
      this.formObj.date = JSON.stringify(this.startTime);
      let rst = await api_updata(this.formObj);
      console.log(rst);
    },
    async query() {
      let rst = await apii_deilta(this.formObj);
      console.log(rst);
      let { data } = rst;
      let {
        id,
        name,
        bulletin,
        avatar,
        deliveryPrice,
        deliveryTime,
        description,
        score,
        sellCount,
        supports,
        date,
        pics,
      } = data;
      this.formObj = {
        id,
        name,
        bulletin,
        avatar,
        deliveryPrice,
        deliveryTime,
        description,
        score,
        sellCount,
        supports,
        date,
        pics,
      };
      this.headerUrl = ` http://localhost:5000/upload/shop/${avatar}`;
      this.fileList = pics.map((item) => {
        return {
          name: item,
          url: ` http://localhost:5000/upload/shop/${item}`,
        };
      });
      this.formSupports = supports;
    },
    //图片上传成功
    handleAvatarSuccess(res, file) {
      console.log(res);
      let { imgUrl } = res;
      this.formObj.avata = imgUrl;
      this.headerUrl = ` http://localhost:5000/upload/shop/${imgUrl}`;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    shopImg_success(res) {
      let { code, imgUrl } = res;
      let fileObj = {
        name: imgUrl,
        url: ` http://localhost:5000/upload/shop/${imgUrl}`,
      };
      this.fileList.push(fileObj);
      this.formObj.pics.push(imgUrl);
    },

    //清楚预览图片
    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.fileList = fileList.map((item) => {
        return { name: item.name, url: item.url };
      });
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
  },
};
</script>

<style lang="less" scoped>
.shop {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>